package org.electrical.controller.functionality;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.electrical.dao.EleCustomerDao;
import org.electrical.dao.EleSaleDao;
import org.electrical.pojo.EleCustomer;
import org.electrical.pojo.EleSale;
import org.electrical.util.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/sale")
@CrossOrigin(value = "*")
public class EleSaleController {

    @Autowired
    private EleSaleDao eleSaleDao;
    @Autowired
    private EleCustomerDao eleCustomerDao;

    @RequestMapping("list")
    @ResponseBody
    public ResponseResult<List<EleSale>> list(@RequestParam(defaultValue = "1") int page, @RequestParam(defaultValue = "10") int limit, EleSale condition) {

        Page<EleSale> pager = PageHelper.startPage(page, limit);
        List<EleSale> list = eleSaleDao.getConditionList(condition);

        return new ResponseResult(pager.getTotal(), list);
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseResult delete(int id) throws Exception {

        eleSaleDao.delete(id);
        return ResponseResult.createSuccessResult();

    }

    @RequestMapping("/batchDelete")
    @ResponseBody
    public ResponseResult batchDelete(Integer[] ids) throws Exception {

        eleSaleDao.batchDelete(ids);
        return ResponseResult.createSuccessResult();

    }

    @RequestMapping("/add")
    @ResponseBody
    public ResponseResult add(EleSale eleSale) throws Exception {

        eleSaleDao.add(eleSale);
        return ResponseResult.createSuccessResult();

    }

    @RequestMapping("/update")
    @ResponseBody
    public ResponseResult update(EleSale eleSale) throws Exception {

        eleSaleDao.update(eleSale);
        return ResponseResult.createSuccessResult();

    }

    @RequestMapping("/addAssign")
    @ResponseBody
    public ResponseResult addAssign(EleSale eleSale) throws Exception {

        eleSaleDao.addAssign(eleSale);
        return ResponseResult.createSuccessResult();

    }

    @RequestMapping("/getAssignSale")
    @ResponseBody
    public ResponseResult<List<EleSale>> getAssignSale(int assignUserId) throws Exception {

        List<EleSale> list = eleSaleDao.getAssignSale(assignUserId);
        return new ResponseResult(list);

    }

    @RequestMapping("/over")
    @ResponseBody
    public ResponseResult over(EleSale eleSale) throws Exception {

        eleSaleDao.upState(eleSale);
        //  如果开发成功，那就添加一个客户，不添加联系人
        if (eleSale.getState() == 3) {
            EleCustomer eleCustomer = new EleCustomer();
            int cusId = eleCustomerDao.getCusId() + 1;
            eleCustomer.setCustomerId("KH09181500" + cusId);
            eleCustomer.setCustomerName(eleSale.getCustomerName());
            eleCustomer.setUserId(eleSale.getAssignUserId());
            eleCustomer.setState(1);
            eleCustomerDao.overAdd(eleCustomer);
        }
        return new ResponseResult(0, "操作成功");

    }

}
